package com.zut.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

/**
 * @author: Mr.Liu
 * @create: 2021-11-13 17:45
 */
@Component
public class DateUtil {

    @Qualifier("myRedisTemplate")
    @Autowired
    RedisTemplate redisTemplate;


    public String getDate(){
        SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
       return sDateFormat.format(new Date());
    }





    public void saveTime() throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        String format = df.format(new Date());
        Date parse = new SimpleDateFormat("yyyy-MM-dd").parse(format);
        Long time = parse.getTime();

        //如果没有这个key
        if(!redisTemplate.boundHashOps("time").hasKey(time.toString())){
            redisTemplate.opsForHash().put("time",time.toString(),1);
        }else {
            redisTemplate.opsForHash().increment("time", time.toString(), 1);
        }
    }

    public Map getTime(){
        return  redisTemplate.opsForHash().entries("time");
    }
}
